package com.thread.exe;

import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;

public class OneShotLatch {
	private final Sync sync = new Sync();
	
	public void signal() {
		sync.releaseShared(0);
	}
	
	public void await() throws InterruptedException {
		sync.acquireSharedInterruptibly(0);
	}
	
	private class Sync extends AbstractQueuedLongSynchronizer{
		private static final long serialVersionUID = 1L;
		
		@Override
		protected long tryAcquireShared(long arg) {
			return (getState() == 1) ? 1 : -1;
		}
		
		@Override
		protected boolean tryReleaseShared(long arg) {
			setState(1);
			return true;
		}
	}
}
